import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 目标：斗地主游戏的案例开发。

 业务需求分析:
 斗地主的做牌, 洗牌, 发牌, 排序（拓展知识）, 看牌。
 业务: 总共有54张牌。
 点数: "3","4","5","6","7","8","9","10","J","Q","K","A","2"
 花色: "♠", "♥", "♣", "♦"
 大小王: "👲" , "🃏"
 点数分别要组合4种花色，大小王各一张。
 斗地主：发出51张牌，剩下3张作为底牌。

 功能：
 1.做牌。
 2.洗牌。
 3.定义3个玩家
 4.发牌。
 5.排序（拓展，了解，作业）
 6.看牌
 */
public class GameDemo {
    //定义牌的集合
    public static List<Card> allCards = new ArrayList<>();
    //使用静态代码块最先运行预加载好牌的信息
    static {
        String[] sizes = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors = {"♠", "♥", "♣", "♦"};
        int index = 0;//专门用来记录牌的大小
        for (String size : sizes) {
            index ++;
            for (String color : colors) {
                Card c = new Card(size,color,index);
                allCards.add(c);
            }
        }
        index++;
        Card c1 = new Card("","👲",index);
        index++;
        Card c2 = new Card("","🃏",index);
        Collections.addAll(allCards,c1,c2);
        System.out.println(allCards);
    }
    public static void main(String[] args) {
        //用collections的接口提供洗牌操作
        Collections.shuffle(allCards);
        System.out.println("洗牌后:"+allCards);

        //定义三个玩家
        List<Card> user1 = new ArrayList<>();
        List<Card> user2 = new ArrayList<>();
        List<Card> user3 = new ArrayList<>();

        //发牌代码,要留三张给地主
        for (int i = 0; i < allCards.size() - 3; i++) {
            Card c = allCards.get(i);
            if(i%3==0){
                user1.add(c);
            }else if(i%3==2){
                user2.add(c);
            }else{
                user3.add(c);
            }
        }
        //剩的3张牌
        List<Card> other = allCards.subList(allCards.size()-3,allCards.size());

        //排序
        Collections.sort(user1, (Card o1, Card o2) -> o1.getIndex()-o2.getIndex());
        Collections.sort(user2, (Card o1, Card o2) -> o1.getIndex()-o2.getIndex());
        Collections.sort(user3, (Card o1, Card o2) -> o1.getIndex()-o2.getIndex());

        System.out.println("玩家1:"+user1);
        System.out.println("玩家2:"+user2);
        System.out.println("玩家3:"+user3);
        System.out.println("还剩3张底牌"+other);
    }
}
